package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.CheckGroupDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.CheckGroup;
import com.itheima.service.CheckGroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service(interfaceClass = CheckGroupService.class)
@Transactional
public class CheckGroupServiceImpl implements CheckGroupService {

    @Autowired
    private CheckGroupDao checkGroupDao;

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();

        PageHelper.startPage(currentPage,pageSize);
        Page<CheckGroup> page = checkGroupDao.queryByCondition(queryString);

        return new PageResult(page.getTotal(), page.getResult());
    }

    @Override
    public void add(CheckGroup checkGroup, Integer[] checkItemIds) {
        //操作新增检查组，操作t_checkgroup表
        checkGroupDao.add(checkGroup);
        //设置检查组和检查项的多对多关联关系，操作t_checkgroup_checkitem表
        this.setCheckGroupAndCheckItem(checkGroup,checkItemIds);
    }

    @Override
    public CheckGroup findById(Integer id) {
        return checkGroupDao.findById(id);
    }

    @Override
    public List<Integer> findCheckItemIdsByCheckGroupId(Integer checkGroupId) {
        return checkGroupDao.findCheckItemIdsByCheckGroupId(checkGroupId);
    }

    @Override
    public void edit(CheckGroup checkGroup, Integer[] checkItemIds) {
        //操作修改检查组，操作t_checkgroup表
        checkGroupDao.edit(checkGroup);
        //清理当前检查组关联的检查项，操作中间关系表t_checkgroup_checkitem表
        checkGroupDao.deleteAssociation(checkGroup.getId());
        //重新建立当前检查组和检查项的关联关系
        setCheckGroupAndCheckItem(checkGroup,checkItemIds);
    }

    @Override
    public void setCheckGroupAndCheckItem(CheckGroup checkGroup, Integer[] checkItemIds){
        //重新建立当前检查组和检查项的关联关系
        Integer checkGroupId = checkGroup.getId();
        if(checkItemIds != null && checkItemIds.length > 0){
            for (Integer checkItemId : checkItemIds) {
                checkGroupDao.setCheckGroupAndCheckItem(checkGroupId,checkItemId);
            }
        }
    }

    @Override
    public void deleteById(Integer id) {
        //判断当前检查组是否已经关联到套餐
        long count = checkGroupDao.findCountByCheckGroupId(id);

        long count2 = checkGroupDao.findCountByCheckGroupId2(id);
        if(count > 0 && count2 > 0){
            //当前检查项已经关联到检查组，不允许删除
            new RuntimeException();
        }
        //不存在关联删除
        checkGroupDao.deleteById(id);
    }

    @Override
    public List<CheckGroup> findAll() {
        return checkGroupDao.findAll();
    }
}
